package reply;

import base.*;
import java.sql.ResultSet;
import java.util.ArrayList;
import reply.RepliesDTO;
import java.sql.PreparedStatement;
import java.sql.Connection;


public class RepliesDAO extends BaseDAO{
    Connection conn ;
   PreparedStatement pstmt;
   ResultSet rs;

   public static void main(String[] args) {
       RepliesDAO repliesdao = new RepliesDAO();
   }

   public ArrayList getAllReplies(int intChannID)
   {
       ArrayList aMessages = new ArrayList();
       try {
           conn = this.getConnection();

           pstmt = conn.prepareStatement("select * from PTips where ChanId=?");
           pstmt.setInt(1, intChannID);
           rs = pstmt.executeQuery();
           while(rs.next())
           {
               RepliesDTO reply = new RepliesDTO();

               int intPId = rs.getInt("PId");
               String strTitle = Common.trim(rs.getString("Topic"));
               String strDateTime = Common.trim(rs.getString("DTime")).substring(0,10);
            //   String strThreadStarter = Common.trim(rs.getString("ThreadStarter"));

               String strBody = Common.trim(rs.getString("Body"));

               reply.setReplyId(intPId);
               reply.setTopic(strTitle);
               reply.setDateTime(strDateTime);
          //     reply.setUserName(strThreadStarter);
               reply.setBody(strBody);

               aMessages.add(reply);
           }
       } catch (Exception ex) {
           ex.printStackTrace();
       }finally
       {
           try {
               this.closeCursors(conn, pstmt);
           } catch (Exception ex1) {
               ex1.printStackTrace();
           }
       }
       return aMessages;

   }

   public ArrayList getAllTopicReplies(int intPID)
   {
       ArrayList aTopicReplies = new ArrayList();
       try {
           conn = this.getConnection();

           pstmt = conn.prepareStatement("select * from PTips where PId=?");
           pstmt.setInt(1, intPID);
           rs = pstmt.executeQuery();
           while(rs.next())
           {
               TopicRepliesDTO reply = new TopicRepliesDTO();

               int intPId = rs.getInt("PId");
               String strTitle = Common.trim(rs.getString("Topic"));
               String strDateTime = Common.trim(rs.getString("DTime")).substring(0,10);
            //   String strThreadStarter = Common.trim(rs.getString("ThreadStarter"));

               String strBody = Common.trim(rs.getString("Body"));

               reply.setPareId(intPId);
               reply.setTopic(strTitle);
               reply.setDTime(strDateTime);
          //     reply.setUserName(strThreadStarter);
               reply.setBody(strBody);

               aTopicReplies.add(reply);
           }
       } catch (Exception ex) {
           ex.printStackTrace();
       }finally
       {
           try {
               this.closeCursors(conn, pstmt);
           } catch (Exception ex1) {
               ex1.printStackTrace();
           }
       }
       return aTopicReplies;

   }


   public boolean addNewReply(RepliesDTO reply)
   {
       boolean bResult = false;
       try
       {
           getConnection();
           conn = this.getConnection();
           String sqlReply = "";

           sqlReply = "insert into PTips (ChanId,UserName,Topic,Body,DTime)" +
                  " values(?,?,?,?,getdate())";

           pstmt = conn.prepareStatement(sqlReply);

           pstmt.setString(1, Integer.toString(reply.getChanId()));
           pstmt.setString(2, reply.getUserName());
           pstmt.setString(3,reply.getTopic());
           pstmt.setString(4, reply.getUserName());

           bResult = pstmt.execute();

       }
       catch (Exception ex)
       {
           ex.printStackTrace();
       }
       finally
       {
           try
           {
               this.closeCursors(conn, pstmt);
           }
           catch (Exception ex1)
           {
               ex1.printStackTrace();
           }
       }

       return bResult;
   }
}
